package goodbaby.dklt.activity;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.classic.core.activity.BaseActivity;
import com.classic.core.activity.BaseActivityStack;
import com.classic.core.utils.ToastUtil;
import com.classic.okhttp.gbb.http.ActionHelp;
import com.classic.okhttp.gbb.http.ObjectCallback;
import com.google.gson.reflect.TypeToken;
import com.squareup.picasso.Picasso;
import com.tgd.gbb.uikit.ui.widget.asyncimageview.CircleImageView;

import java.lang.reflect.Type;
import java.util.Timer;

import goodbaby.dklt.GbbCache;
import goodbaby.dklt.GbbPreferences;
import goodbaby.dklt.R;
import goodbaby.dklt.bean.PowerBean;

/**
 * Created by Jayden on 2016/4/23 18:50.
 *
 * @version V1.0
 * @Title: UserActivity
 * @Description: 个人中心页面
 */
public class UserActivity extends BaseActivity {

    private static final int REQUEST_ENABLE_BT = 2;
    private static final long SCAN_PERIOD = 10000L;
    public static TextView tvFindResult;
    private BaseActivityStack activityStack;
    private Intent bindIntent;
    private Button btnLoginOut;
    private String className;
    private int isFind = 0;
    private ImageView ivBack;
    private CircleImageView ivHeadImg;
    private BluetoothAdapter mBtAdapter = null;
    private BluetoothDevice mDevice = null;
    private Handler mHandler;
    private Timer mTimer;
    private String macAddress;
    private String nickName;
    private String power;
    private RelativeLayout rlAbout;
    private RelativeLayout rlFindandUpdate;
    private RelativeLayout rlSettingUserInfo;
    private String school;
    private TextView tvMacAddress;
    private TextView tvName;
    private TextView tvQty;
    private TextView tvSchoolandClass;
    private TextView tvTitle;
    private String userImgPath;

    private void clearUserInfo() {
        ActionHelp.userLoginOut(this, new ObjectCallback<String>() {

            public void onError(int paramAnonymousInt) {
            }

            public void onSuccess(String paramAnonymousString) {
                GbbCache.clear();
                Intent localIntent = new Intent(UserActivity.this, LoginActivity.class);
                startActivity(localIntent);
                activityStack.appExit(UserActivity.this);
                ToastUtil.showToast(UserActivity.this, "退出成功");
            }

            @Override
            public Type getType() {
                return new TypeToken<String>() {
                }.getType();
            }
        });
    }


    private void getQty() {
        ActionHelp.gbbSelectQty(this, new ObjectCallback<PowerBean>() {


            public void onError(int paramAnonymousInt) {
            }

            public void onSuccess(PowerBean paramAnonymousPowerBean) {
                power = paramAnonymousPowerBean.getPowerValue();
                setData();
            }

            @Override
            public Type getType() {
                return new TypeToken<PowerBean>() {
                }.getType();
            }
        });
    }


    private void setData() {
        Picasso.with(this).load(userImgPath).into(ivHeadImg);
        tvName.setText(nickName);
        tvSchoolandClass.setText(school + "\b\b" + className);
        tvMacAddress.setText(macAddress);
        tvQty.setText(power + "%");
    }


    public int getLayoutResId() {
        return R.layout.activity_user;
    }

//    public byte[] getShockValue() {
//        StringBuffer localStringBuffer = new StringBuffer();
//        localStringBuffer.append("ab").append("00000700000000").append("01").append("000800020000");
//        return StringUtil.HexString2Bytes(localStringBuffer.toString());
//    }

    public void initData() {
        super.initData();
        activityStack = BaseActivityStack.getInstance();
//        mTimer = new Timer();
//        mHandler = new Handler();
//        mBtAdapter = ((BluetoothManager) getSystemService("bluetooth")).getAdapter();
//        if (!this.mBtAdapter.isEnabled()) {
//            Intent localIntent = new Intent("android.bluetooth.adapter.action.REQUEST_ENABLE");
//            Log.v("BluetoothAdapter", "BluetoothAdapter.ACTION_REQUEST_ENABLE");
//            startActivityForResult(localIntent, 2);
//        }
//        if (this.mBtAdapter == null) {
//            ToastUtil.showToast(this, "蓝牙不可用");
//            finish();
//            return;
//        }
        userImgPath = GbbPreferences.getHeadImg();
        nickName = GbbPreferences.getNickName();
        school = GbbPreferences.getSchoolName();
        className = GbbPreferences.getClassName();
        macAddress = GbbPreferences.getBraceletAddress();
    }

    public void initView() {
        super.initView();
        tvTitle = (TextView) findViewById(R.id.tv_common_title);
        tvTitle.setText("个人中心");
        ivBack = (ImageView) findViewById(R.id.iv_common_back);
        ivBack.setVisibility(View.VISIBLE);
        ivBack.setOnClickListener(this);
        rlAbout = (RelativeLayout) findViewById(R.id.rl_about);
        rlAbout.setOnClickListener(this);
        rlFindandUpdate = (RelativeLayout) findViewById(R.id.rl_findandupdate);
        rlFindandUpdate.setOnClickListener(this);
        rlSettingUserInfo = (RelativeLayout) findViewById(R.id.rl_setting_userinfo);
        rlSettingUserInfo.setOnClickListener(this);
        btnLoginOut = (Button) findViewById(R.id.btn_loginout);
        btnLoginOut.setOnClickListener(this);
        ivHeadImg = (CircleImageView) findViewById(R.id.iv_user_headimg);
        tvName = (TextView) findViewById(R.id.tv_user_name);
        tvSchoolandClass = (TextView) findViewById(R.id.tv_schoolandclass);
        tvQty = (TextView) findViewById(R.id.tv_qty);
        tvMacAddress = (TextView) findViewById(R.id.tv_macaddress);
        tvFindResult = (TextView) findViewById(R.id.tv_find_result);
        getQty();
        setData();
    }

    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
    }

    protected void onDestroy() {
        super.onDestroy();
//        try {
//            LocalBroadcastManager.getInstance(this).unregisterReceiver(this.UARTStatusChangeReceiver);
//            unbindService(this.mServiceConnection);
//            this.mService.stopSelf();
//            this.mService = null;
//            this.mScanning = false;
//            this.mBtAdapter.stopLeScan(this.mLeScanCallback);
//            return;
//        } catch (Exception localException) {
//            Log.e("", localException.toString());
//        }
    }

    protected void onResume() {
        super.onResume();
        userImgPath = GbbPreferences.getHeadImg();
        nickName = GbbPreferences.getNickName();
        setData();
    }

    public void viewClick(View paramView) {
        super.viewClick(paramView);
        switch (paramView.getId()) {
            default:
                break;
            case R.id.iv_common_back:
                finish();
                break;
            case R.id.rl_about:
                startActivity(new Intent(this, AboutActivity.class));
                break;
            case R.id.rl_setting_userinfo:
                startActivity(new Intent(this, UserInfoActivity.class));
                break;
            case R.id.rl_findandupdate:
                tvFindResult.setText("开始查找");
//                scanLeDevice(true);
                return;
            case R.id.btn_loginout:
                clearUserInfo();
                break;
        }
    }
}
